﻿using System;
using System.Html;
using enyo;
using onyx;

namespace JenkinsView
{
    public class JobListView : enyo.FittableRows, IObserver
    {
        [EvalAtCompile, NoCompile]
        private const string LST_JOB = "lstJob";
        [EvalAtCompile, NoCompile]
        private const string LBL_JOB = "lblJob";
        [EvalAtCompile, NoCompile]
        private const string LBL_JOB_DESC = "lblJobDesc";
        [EvalAtCompile, NoCompile]
        private const string ICN_JOB = "icnJob";
        [EvalAtCompile, NoCompile]
        private const string ITM_JOB = "itmJob";
        

        [EvalAtCompile, NoCompile]
        private const string LBL_TITLE = "lblTitle";

        protected new string Classes = CssConst.ONYX;

        private new Component[] Components = new Component[]
        {
            new Toolbar() { Kind = onyx.ComponentKind.Toolbar, LayoutKind = enyo.LayoutKind.FittableColumnsLayout, NoStretch = true,
                Components = new Component[]
                {
                    new onyx.IconButton(){ Kind = onyx.ComponentKind.IconButton, Src = ImgSrc.CANCEL, ontap = "navigateBack" },
                    new Control() { Fit = true, Name = LBL_TITLE, Content = "[No Server Selected]", Classes = CssConst.TOOLBAR_TITLE },
                    new onyx.IconButton(){ Kind = onyx.ComponentKind.IconButton, Src = ImgSrc.REFRESH, ontap = "refreshJobList" }
                }
            },
            
            new List() { Kind = enyo.ComponentKind.List, Fit = true, Name = LST_JOB, Touch = true, OnSetupItem = "setupItem",
                Components = new Component[]
                {
                    new Item(){ Name = ITM_JOB, Classes = CssConst.PANELS_ITEM, ontap = "jobSelected",// NoStretch = true,
                        Components = new Component[]{
                            new Control(){ LayoutKind = enyo.LayoutKind.FittableColumnsLayout, NoStretch = true,
                                Components = new Component[]
                                {
                                    new Image(){ Kind = enyo.ComponentKind.Image, Name = ICN_JOB, Classes = CssConst.SCORE_ICON },
                                    new Control(){Name = LBL_JOB},
                                }
                            },
                            new Control(){Name = LBL_JOB_DESC, Classes = CssConst.JOB_DESC},
                        }
                    }
                }
            }
        };

        private C FindControl<C>(string name) where C : class
        {
            return this._[name] as C;
        }

        public override void Create()
        {
            this.Inherited(Arguments.Value);
            DomainModel.Model.JobManager.SetObserver(this);
        }

        private void NavigateBack()
        {
            DomainModel.Model.NotifyAll(DomainModel.PANEL_NAVIGATE, DomainModel.PREV_VIEW);
        }

        private void SetupItem(Control c, SetupRowEventArgs arg)
        {
            var job = DomainModel.Model.JobManager.ListJobs()[arg.Index];
            var lbl = this.FindControl<Control>(LBL_JOB);
            lbl.Content = job.Name;

            var item = this.FindControl<Control>(ITM_JOB);
            var list = this.FindControl<List>(LST_JOB);
            item.AddRemoveClass(CssConst.PANELS_ITEM_SELECTED, list.IsSelected(arg.Index));

            HealthReportData h = job.HealthReport[0];
            var lblDesc = this.FindControl<Control>(LBL_JOB_DESC);
            lblDesc.Content = "Buildable: [" + job.Buildable + "] " + ((h != null) ? h.Description : "");
            var icn = this.FindControl<Image>(ICN_JOB);
            icn.Src = Score2IconUrl((h != null) ? h.Score : -1);
        }

        private string Score2IconUrl(int score)
        {
            if (score < 0)
                return "";
            if (score < 20)
                return "assets/0.gif";
            if (score < 40)
                return "assets/1.gif";
            if (score < 60)
                return "assets/2.gif";
            if (score < 80)
                return "assets/3.gif";
            return "assets/4.gif";
        }

        private void RefreshJobList()
        {
            Console.Log("Refresh Job List.");
            DomainModel.Model.JobManager.ReadJobsData(true);
        }

        private void JobSelected(Control c, EnyoEventArgs arg)
        {
            int idx = (int)arg["index"];
            Console.Log("Get Job Data: " + idx);
            DomainModel.Model.JobManager.SelectedJobIndex = idx;
            DomainModel.Model.JobDetailManager.ReadJobDetailData(false);
        }

        public void Update(int reason, object arg)
        {
            switch (reason)
            {
                case DomainModel.JOB_LIST_CHANGE:
                    Console.Log("Update JobListView, reason:" + reason);
                    
                    var lbl = this.FindControl<Control>(LBL_TITLE);
                    lbl.Content = "Server: [" + DomainModel.Model.ServerManager.ListCurrentServer().SvrName + "]";

                    var lst = this.FindControl<List>(LST_JOB);
                    lst.Count = (int)DomainModel.Model.JobManager.ListJobs().Length;
                    lst.Refresh();
                    break;
            }
        }
    }
}
